﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Diagnostics;

namespace MDIDemo.Modules
{
    public partial class GiaBan : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        int idSua = 20;

        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnLuu.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);

            }
        }
        public GiaBan()
        {
            InitializeComponent();

        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            Close();
        }
        void EnabledForm(bool p)
        {
            colGiaKhac.OptionsColumn.AllowEdit = p;
            colGiaSy.OptionsColumn.AllowEdit = p;
            colGiaLe.OptionsColumn.AllowEdit = p;
            btnLuu.Enabled = p;
            btnBoQua.Enabled = p;
            btnSua.Enabled = !p;
            btnExcel.Enabled = !p;
            tlHangHoa.Enabled = !p;
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            if (gridPrice.DataSource != null)
                EnabledForm(true);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            DataTable dt = gridPrice.DataSource as DataTable;
            if (dt != null)
            {
                BUS.TONKHOControl ctr = new BUS.TONKHOControl();
                foreach (DataRow row in dt.Rows)
                {
                    if (row.RowState == DataRowState.Modified)
                    {
                        DTO.TONKHOInfo inf = new DTO.TONKHOInfo(row);
                        inf.CREATEBY = frmMain.__userID;
                        inf.MODIFIEDBY = frmMain.__userID;
                        inf.MODIFIEDDATE = DateTime.Now;
                        inf.CREATEDATE = DateTime.Now;
                        sErr += ctr.InsertUpdate(inf);


                    }
                }
                if (sErr == "")
                    XtraMessageBox.Show("Lưu thành công", "Thông báo");
                else XtraMessageBox.Show(sErr, "Lỗi");
            }
            EnabledForm(false);
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            EnabledForm(false);
        }

        private void GiaBan_Load(object sender, EventArgs e)
        {
            BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
            tlHangHoa.DataSource = ctr.GetTreeList("", ref sErr);
            tlHangHoa.ExpandAll();
            EnabledForm(false);
            SetPermission();
        }

        private void tlHangHoa_SelectionChanged(object sender, EventArgs e)
        {

            //gridPrice.DataSource = ctr.GetTonKho()
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel file(*.xls)|*.xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                gridPrice.ExportToXls(sfd.FileName);
                if (XtraMessageBox.Show("Bạn có muốn mở file vừa tạo?", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    Process.Start(sfd.FileName);
            }
        }

        private void GiaBan_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
            }
          
        }

        private void gridPrice_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
        }

        private void tlHangHoa_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (e.Node != null)
            {
                BUS.LOAIHANGControl ctr = new BUS.LOAIHANGControl();
                if (e.Node.ParentNode != null)
                {
                    gridPrice.DataSource = ctr.GetTonKho(e.Node.GetValue("ParentID").ToString(), e.Node.GetValue("ID").ToString(), ref sErr);
                }
                else gridPrice.DataSource = null;
            }


        }
    }
}